import pygame
from pygame.sprite import Sprite

from game_stats import GameStats
from settings import Settings
from ship import Ship


class Bullet(Sprite):
    """管理从船上射出的子弹的类。"""

    def __init__(self, ai_settings: Settings, screen: pygame.SurfaceType, ship: Ship):
        """在船的当前位置创建一个子弹物体。"""
        super().__init__()
        self.screen = screen
        # 在(0,0)处创建一个项目符号矩形，然后设置其位置。
        self.rect = pygame.Rect(0, 0, ai_settings.bullet_width,ai_settings.bullet_height)
        self.rect.centerx = ship.rect.centerx
        self.rect.top = ship.rect.centery

        # 将子弹的位置存储为浮动。
        self.y = float(self.rect.y)
        self.x = float(self.rect.x)
        self.color = ai_settings.bullet_color
        self.speed_factor = ai_settings.bullet_speed_factor

    def update(self, stats: GameStats):
        """移动屏幕上的子弹。"""
        # 子弹上移改为子弹右移
        # self.y -= self.speed_factor * stats.time_passed
        # self.rect.y = self.y
        self.x += self.speed_factor * stats.time_passed
        self.rect.x = self.x

    def draw_bullet(self):
        """在屏幕上画子弹。"""
        pygame.draw.rect(self.screen, self.color, self.rect)
